/**
 * 添加或者修改页面
 */
var FlowNewsCaseInfoDlg = {
    data: {
        newsCaseId: "",
        serviceType: "",
        newsTypeId: "",
        orderCode: "",
        serviceName: "",
        title: "",
        notes: "",
        cover: "",
        watch: "",
        detail: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;





    //普通图片上传
    var upload = layui.upload;
    upload.render({
        elem: '#coverBtn'
        ,url: Feng.ctxPath + '/system/upload' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            $("#cover").val("/upload/"+res.data.fileName);
            Feng.success(res.message);
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });




    //实例化编辑器
    var ue = UE.getEditor('detail', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/ueditor/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };





    //类型
    var newsTypeAjax = new $ax(Feng.ctxPath + "/flowNewsProblem/newsType");
    var newsTypeResult = newsTypeAjax.start();
    if(newsTypeResult.code == 200){
        var newsTypeStr = "<option value=''>请选择文章类型</option>";
        for (var i = 0;i<newsTypeResult.data.length;i++){
            newsTypeStr += "<option value='"+newsTypeResult.data[i].newsTypeId+"'>"+newsTypeResult.data[i].newsTypeName+"</option>";
        }
        $("#newsTypeId").html(newsTypeStr);
        form.render('select');
    }

    //类型
    var serviceTypeAjax = new $ax(Feng.ctxPath + "/flowNewsProblem/serviceType");
    var serviceTypeResult = serviceTypeAjax.start();
    if(serviceTypeResult.code == 200){
        var serviceTypeStr = "<option value=''>请选择服务类型</option>";
        for (var i = 0;i<serviceTypeResult.data.length;i++){
            serviceTypeStr += "<option value='"+serviceTypeResult.data[i].serviceId+"'>"+serviceTypeResult.data[i].serviceName+"</option>";
        }
        $("#serviceType").html(serviceTypeStr);
        form.render('select');
    }





















    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        data.field.detail = encodeURIComponent(data.field.detail);
        var ajax = new $ax(Feng.ctxPath + "/flowNewsCase/addItem", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


});